﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class ToCHNMoney
    {
        public static string DoubleToCHNMoney(double MoneySource)
        {
            Int64 Money = Convert.ToInt64(MoneySource * 100);
            string[] Cstr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            string[] Wstr = { "分", "角", "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
            string MoneyStr = Money.ToString();
            string BigMoney = "";
            int di, dj = MoneyStr.Length;
            for (di = 0; di < dj; di++)
            {
                int tm = Convert.ToInt16(MoneyStr.Substring(dj - di - 1, 1));
                BigMoney += Wstr[di] + Cstr[tm];
            }
            string bm = string.Empty;
            foreach (char a in BigMoney.ToCharArray())
            {
                bm = a + bm;
            }
            for (di = 0; di < dj; di++)
            {
                bm = bm.Replace("零仟零佰零拾零圆", "圆");
                bm = bm.Replace("零仟零佰零拾零万", "万");
                bm = bm.Replace("零零", "零");
                bm = bm.Replace("零圆", "圆");
                bm = bm.Replace("零拾", "零");
                bm = bm.Replace("零佰", "零");
                bm = bm.Replace("零仟", "零");
                bm = bm.Replace("零万", "万");
                bm = bm.Replace("零亿", "亿");
                bm = bm.Replace("零角零分", "整");
                bm = bm.Replace("零分", "");
                bm = bm.Replace("零角", "");
            }
            return bm;
        }

        public static string StringToCHNMoney(string MoneySource)
        {
            double tM = Double.Parse(MoneySource);
            Int64 Money = Convert.ToInt64(tM * 100);
            string[] Cstr = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            string[] Wstr = { "分", "角", "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
            string MoneyStr = Money.ToString();
            string BigMoney = "";
            int di, dj = MoneyStr.Length;
            for (di = 0; di < dj; di++)
            {
                int tm = Convert.ToInt16(MoneyStr.Substring(dj - di - 1, 1));
                BigMoney += Wstr[di] + Cstr[tm];
            }
            string bm = string.Empty;
            foreach (char a in BigMoney.ToCharArray())
            {
                bm = a + bm;
            }
            for (di = 0; di < dj; di++)
            {
                bm = bm.Replace("零仟零佰零拾零圆", "圆");
                bm = bm.Replace("零仟零佰零拾零万", "万");
                bm = bm.Replace("零零", "零");
                bm = bm.Replace("零圆", "圆");
                bm = bm.Replace("零拾", "零");
                bm = bm.Replace("零佰", "零");
                bm = bm.Replace("零仟", "零");
                bm = bm.Replace("零万", "万");
                bm = bm.Replace("零亿", "亿");
                bm = bm.Replace("零角零分", "整");
                bm = bm.Replace("零分", "");
                bm = bm.Replace("零角", "");
            }
            return bm;
        }
    }
}
